package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;

/**
 * 1、所有系列信息
 * 
 * @author Tallen Zhai
 * 
 */
public class SerieListEntity extends BaseEntity {

	private static final long serialVersionUID = 2717167378367248608L;

	public List<SerieEntity> dataList = null;

	public String statusCode = "";
	public String msg = "";
	public final static String STATUS_CODE = "statusCode";
	public final static String MSG = "msg";
	private final static String SERIES = "series";

	public static SerieListEntity parse(JSONObject jsonObject, Context activity)
			throws JSONException {
		SerieListEntity bean = new SerieListEntity();
		if (jsonObject.has(STATUS_CODE)) {
			bean.statusCode = jsonObject.getString(STATUS_CODE);
		}
		if (jsonObject.has(MSG)) {
			bean.msg = jsonObject.getString(MSG);
		}
		/**
		 * 解析series节点
		 */
		if (jsonObject.has(SERIES)) {

			JSONArray jsonArray = jsonObject.getJSONArray(SERIES);

			bean.dataList = new ArrayList<SerieEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				SerieEntity entity = SerieEntity.parse(object, activity);
				bean.dataList.add(entity);
			}

		}

		return bean;
	}

	public static class SerieEntity extends BaseEntity {

		private static final long serialVersionUID = -1184120074012623470L;
		public String id = "";
		public String name = "";
		public String seriesTypeName = "";// 所属类型

		public final static String ID = "id";
		public final static String NAME = "name";

		// =========页面中才用的到=========
		public boolean isOff = true;
		public List<BookEntity> dataList = null;
		public boolean isSelected = false;

		public List<ChildSerieEntity> childList = null;

		public static SerieEntity parse(JSONObject jsonObject, Context activity)
				throws JSONException {
			SerieEntity bean = new SerieEntity();
			if (jsonObject.has(ID)) {
				bean.id = jsonObject.getString(ID);
			}
			if (jsonObject.has(NAME)) {
				bean.name = jsonObject.getString(NAME);
			}

			if (jsonObject.has("seriesType")) {
				bean.seriesTypeName = jsonObject.getString("seriesType");
			}
			return bean;
		}

	}

	// =========页面中才用的到=========
	/**
	 * 子系列实体，及子系列下的BookEntity
	 * @author 390694
	 *
	 */
	public static class ChildSerieEntity extends BaseEntity {
		private static final long serialVersionUID = 8090264377822604019L;
		public String childSeriesId = "";
		public String childSeriesName = "";
		// =========页面中才用的到=========
		public String seriesName = "";
		public int childSeriesCount = 0;
		/**
		 * 同一个childSeriesId下可能对应N个BookEntity
		 */
		public List<BookEntity> dataList = null;
		public boolean isSelected = false;

		public boolean isOff = true;

		public String seriesId = "";
		public ArrayList<String> childSeriesBookIdList = new ArrayList<String>();

		public final static String CHILD_SERIES_ID = "childSeriesId";
		public final static String CHILD_SERIES_NAME = "childSeriesName";
		public final static String SERIES_ID = "seriesId";

		public final static String CHILD_SERIES_BOOK_ID_LIST = "childSeriesBookIdList";

		public static ChildSerieEntity parse(JSONObject jsonObject)
				throws JSONException {
			ChildSerieEntity bean = new ChildSerieEntity();
			if (jsonObject.has(CHILD_SERIES_ID)) {
				bean.childSeriesId = jsonObject.getString(CHILD_SERIES_ID);
			}
			if (jsonObject.has(CHILD_SERIES_NAME)) {
				bean.childSeriesName = jsonObject.getString(CHILD_SERIES_NAME);
			}
			if (jsonObject.has(SERIES_ID)) {
				bean.seriesId = jsonObject.getString(SERIES_ID);
			}
			/**
			 * 解析childSeriesBookIdList节点
			 */
			if (jsonObject.has(CHILD_SERIES_BOOK_ID_LIST)) {

				JSONArray jsonArray = jsonObject
						.getJSONArray(CHILD_SERIES_BOOK_ID_LIST);

				bean.childSeriesBookIdList = new ArrayList<String>();
				for (int i = 0; i < jsonArray.length(); i++) {
					String bookId = jsonArray.getString(i);
					bean.childSeriesBookIdList.add(bookId);
				}

			}
			return bean;
		}
	}
}
